package com.network.topo.snmp;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import org.snmp4j.CommunityTarget;
import org.snmp4j.Target;
import org.snmp4j.UserTarget;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.OctetString;

import com.network.util.snmp4j.SnmpGet;
import com.network.util.snmp4j.SnmpGetResultSet;

public final class OspfNbrTable extends AbstractXxxTable
{
	private static ArrayList<String> oidList = null;
	private HashMap<Object, RowData> objDataMap = new HashMap<Object, RowData>();
	private HashMap<Object, RowData> indexDataMap = new HashMap<Object, RowData>();
	private SnmpGet snmp4j = null;
	private int rowCount = 0;
	private Target target = null;

	private OspfNbrTable(SnmpGetResultSet resultSet)
	{
		this.resultSet = resultSet;
		if (this.resultSet != null)
		{
			rowCount = this.resultSet.getRowCount();
			for (int i = 0; i < rowCount; i++)
			{
				RowData rowData = new RowData();
				int oidIndex = 0;
				rowData.ospfNbrIpAddr = getValue(getOidList().get(oidIndex++), i);
				rowData.ospfNbrRtrId = getValue(getOidList().get(oidIndex++), i);
				rowData.ospfNbrState = str2int(getValue(getOidList().get(oidIndex++), i));
				
				indexDataMap.put(i, rowData);
			}
		}
	}

	private RowData getRowData(int row)
	{
		RowData rowData = new RowData();
		
		rowData.ospfNbrIpAddr = getOspfNbrIpAddr(row, true);
		rowData.ospfNbrRtrId = getOspfNbrRtrId(row, true);
		rowData.ospfNbrState = getOspfNbrState(row, true);
		
		return rowData;
	}

	public static OspfNbrTable getInstance(SnmpGet snmpGet, Target target)
	{
		SnmpGetResultSet sgrs = null;
		OspfNbrTable instance = null;
		try
		{
			sgrs = snmpGet.query(getOidList(), target);
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		if (sgrs != null && sgrs.next() == true)
		{
			instance = new OspfNbrTable(sgrs);
		}

		return instance;
	}

	public static synchronized ArrayList<String> getOidList()
	{
		if (oidList == null)
		{
			oidList = new ArrayList<String>();
			oidList.add("1.3.6.1.2.1.14.10.1.1");
			oidList.add("1.3.6.1.2.1.14.10.1.3");
			oidList.add("1.3.6.1.2.1.14.10.1.6");
		}
		return oidList;
	}
	
	protected static Target newTarget(SnmpGet snmpGet, String ipAddr, ArrayList<String> communityList, int snmpVersion) throws IOException
	{
		Target target = null;
		Address addr = SnmpGet.parserAddress("udp:" + ipAddr);
//		_log.info("snmpVersion:" + snmpVersion);
		if (addr == null)
		{
			return null;
		}
		if (snmpVersion == SnmpConstants.version1)
		{
			target = new CommunityTarget();
			target.setVersion(SnmpConstants.version1);
		}
		else if (snmpVersion == SnmpConstants.version2c)
		{
			target = new CommunityTarget();
			target.setVersion(SnmpConstants.version2c);
		}
		else if (snmpVersion == SnmpConstants.version3)
		{
			target = new UserTarget();
			target.setVersion(SnmpConstants.version3);
		}
		target.setAddress(addr);
		boolean isFindCommunity = false;
		ArrayList<String> oidList = new ArrayList<String>();
		oidList.add(SysInfo.getOidList().get(1));
		for (String community : communityList)
		{
			if (target instanceof CommunityTarget)
			{
				CommunityTarget objTemp = (CommunityTarget) target;
				objTemp.setCommunity(new OctetString(community));
			}
			if (snmpGet.query(oidList, target) != null)
			{
				isFindCommunity = true;
				break;
			}
		}
		if (isFindCommunity == false)
		{
			target = null;
			addr = null;
			// throw new IOException("community list configure error!");
		}
		return target;
	}

	public String getOspfNbrIpAddr(Object obj, boolean useIndex)
	{
		String rtnValue = "127.0.0.1";
		RowData rowData = null;
		if (useIndex)
		{
			rowData = indexDataMap.get(obj);
		}
		else
		{
			rowData = objDataMap.get(obj);
		}
		if (rowData != null)
		{
			rtnValue = rowData.getOspfNbrIpAddr();
		}
		return rtnValue;
	}

	public String getOspfNbrRtrId(Object obj, boolean useIndex)
	{
		String rtnValue = "127.0.0.1";
		RowData rowData = null;
		if (useIndex)
		{
			rowData = indexDataMap.get(obj);
		}
		else
		{
			rowData = objDataMap.get(obj);
		}
		if (rowData != null)
		{
			rtnValue = rowData.getOspfNbrRtrId();
		}
		return rtnValue;
	}
	
	public int getOspfNbrState(Object obj, boolean useIndex)
	{
		int rtnValue = 0;
		RowData rowData = null;
		if (useIndex)
		{
			rowData = indexDataMap.get(obj);
		}
		else
		{
			rowData = objDataMap.get(obj);
		}
		if (rowData != null)
		{
			rtnValue = rowData.getOspfNbrState();
		}
		return rtnValue;
	}

	
	public class RowData
	{
		private String ospfNbrIpAddr = "";
		private String ospfNbrRtrId = "";
		private int ospfNbrState = 0;
		
		public String getOspfNbrIpAddr() {
			return ospfNbrIpAddr;
		}
		
		public void setOspfNbrIpAddr(String ospfNbrIpAddr) {
			this.ospfNbrIpAddr = ospfNbrIpAddr;
			if (this.ospfNbrIpAddr == null || this.ospfNbrIpAddr.split("[.]").length != 4)
			{
				this.ospfNbrIpAddr = "127.0.0.1";
			}
		}
		
		public String getOspfNbrRtrId() {
			return ospfNbrRtrId;
		}
		
		public void setOspfNbrRtrId(String ospfNbrRtrId) {
			this.ospfNbrRtrId = ospfNbrRtrId;
			this.ospfNbrRtrId = ospfNbrRtrId;
			if (this.ospfNbrRtrId == null || this.ospfNbrRtrId.split("[.]").length != 4)
			{
				this.ospfNbrRtrId = "127.0.0.1";
			}
		}
		
		public int getOspfNbrState() {
			return ospfNbrState;
		}
		
		public void setOspfNbrState(int ospfNbrState) {
			this.ospfNbrState = ospfNbrState;
		}
		

	}

	public int getRowCount()
	{
		return rowCount;
	}

	public void setRowCount(int rowCount)
	{
		this.rowCount = rowCount;
	}

	public Target getTarget() {
		return target;
	}

	public void setTarget(Target target) {
		this.target = target;
	}

	public SnmpGet getSnmp4j() {
		return snmp4j;
	}

	public void setSnmp4j(SnmpGet snmp4j) {
		this.snmp4j = snmp4j;
	}
}
